<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>重采样拷贝部分图像并调整大小</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.imagecopyresampled" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">imagecopyresampled</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">imagecopyresampled</span> &mdash; <span class="dc-title">重采样拷贝部分图像并调整大小</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.imagecopyresampled-description">
  <h3 class="title">说明</h3>
 <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>imagecopyresampled</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$dst_image</code></span>
   , <span class="methodparam"><span class="type">resource</span> <code class="parameter">$src_image</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$dst_x</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$dst_y</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$src_x</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$src_y</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$dst_w</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$dst_h</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$src_w</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$src_h</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment"> <span class="function"><strong>imagecopyresampled()</strong></span>
   将一幅图像中的一块正方形区域拷贝到另一个图像中，平滑地插入像素值，因此，尤其是，减小了图像的大小而仍然保持了极大的清晰度。
  </p>
  <p class="para">
    In other words, <span class="function"><strong>imagecopyresampled()</strong></span> will take a 
   rectangular area from <code class="parameter">src_image</code> of width 
   <code class="parameter">src_w</code> and height <code class="parameter">src_h</code> at 
   position (<code class="parameter">src_x</code>,<code class="parameter">src_y</code>) 
   and place it in a rectangular area of <code class="parameter">dst_image</code> 
   of width <code class="parameter">dst_w</code> and height <code class="parameter">dst_h</code>
   at position (<code class="parameter">dst_x</code>,<code class="parameter">dst_y</code>).
  </p>
  <p class="para">
   如果源和目标的宽度和高度不同，则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝（如果
   <code class="parameter">dst_image</code> 和 <code class="parameter">src_image</code>
   相同的话）区域，但如果区域交迭的话则结果不可预知。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.imagecopyresampled-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">dst_image</code></dt>

     <dd>

      <p class="para">目标图象连接资源。</p>
     </dd>

    
    
     <dt>
<code class="parameter">src_image</code></dt>

     <dd>

      <p class="para">源图象连接资源。</p>
     </dd>

    
    
     <dt>
<code class="parameter">dst_x</code></dt>

     <dd>

      <p class="para">
       目标 X 坐标点。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">dst_y</code></dt>

     <dd>

      <p class="para">
       目标 Y 坐标点。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">src_x</code></dt>

     <dd>

      <p class="para">
       源的 X 坐标点。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">src_y</code></dt>

     <dd>

      <p class="para">
       源的 Y 坐标点。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">dst_w</code></dt>

     <dd>

      <p class="para">
       目标宽度。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">dst_h</code></dt>

     <dd>

      <p class="para">
       目标高度。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">src_w</code></dt>

     <dd>

      <p class="para">源图象的宽度。</p>
     </dd>

    
    
     <dt>
<code class="parameter">src_h</code></dt>

     <dd>

      <p class="para">源图象的高度。</p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.imagecopyresampled-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.imagecopyresampled-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3673">
    <p><strong>Example #1 简单的例子</strong></p>
    <div class="example-contents"><p>
     这个例子会将图像调整为原有尺寸的一半。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;这个文件<br /></span><span style="color: #0000BB">$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test.jpg'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$percent&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0.5</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;内容类型<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type:&nbsp;image/jpeg'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;获取新的尺寸<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$width</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$height</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$new_width&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$width&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$percent</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$new_height&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$height&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$percent</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;重新取样<br /></span><span style="color: #0000BB">$image_p&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecreatetruecolor</span><span style="color: #007700">(</span><span style="color: #0000BB">$new_width</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$new_height</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$image&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecreatefromjpeg</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagecopyresampled</span><span style="color: #007700">(</span><span style="color: #0000BB">$image_p</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$image</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$new_width</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$new_height</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$width</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$height</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;输出<br /></span><span style="color: #0000BB">imagejpeg</span><span style="color: #007700">(</span><span style="color: #0000BB">$image_p</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="mediaobject">
     
     <div class="imageobject">
      <img src="images/21009b70229598c6a80eef8b45bf282b-imagecopyresampled.jpg" alt="输出的例子：简单的例子" width="47" height="25" />
     </div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3674">
    <p><strong>Example #2 按比例对图像重新采样</strong></p>
    <div class="example-contents"><p>
     这个例子会以最大宽度高度为 200 像素显示一个图像。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;源文件<br /></span><span style="color: #0000BB">$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test.jpg'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;设置最大宽高<br /></span><span style="color: #0000BB">$width&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">200</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$height&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">200</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Content&nbsp;type<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type:&nbsp;image/jpeg'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;获取新尺寸<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$width_orig</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$height_orig</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$ratio_orig&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$width_orig</span><span style="color: #007700">/</span><span style="color: #0000BB">$height_orig</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">$width</span><span style="color: #007700">/</span><span style="color: #0000BB">$height&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$ratio_orig</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$width&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$height</span><span style="color: #007700">*</span><span style="color: #0000BB">$ratio_orig</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$height&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$width</span><span style="color: #007700">/</span><span style="color: #0000BB">$ratio_orig</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;重新取样<br /></span><span style="color: #0000BB">$image_p&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecreatetruecolor</span><span style="color: #007700">(</span><span style="color: #0000BB">$width</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$height</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$image&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecreatefromjpeg</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagecopyresampled</span><span style="color: #007700">(</span><span style="color: #0000BB">$image_p</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$image</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$width</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$height</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$width_orig</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$height_orig</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;输出<br /></span><span style="color: #0000BB">imagejpeg</span><span style="color: #007700">(</span><span style="color: #0000BB">$image_p</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="mediaobject">
     
     <div class="imageobject">
      <img src="images/21009b70229598c6a80eef8b45bf282b-imagecopyresampled_2.jpg" alt="输出例子：按比例对图像重新采样" width="200" height="107" />
     </div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.imagecopyresampled-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
     因为调色板图像限制（255+1 种颜色）有个问题。重采样或过滤图像通常需要多于 255
    种颜色，计算新的被重采样的像素及其颜色时采用了一种近似值。对调色板图像尝试分配一个新颜色时，如果失败我们选择了计算结果最接近（理论上）的颜色。这并不总是视觉上最接近的颜色。这可能会产生怪异的结果，例如空白（或者视觉上是空白）的图像。要跳过这个问题，请使用真彩色图像作为目标图像，例如用
    <span class="function"><a href="imagecreatetruecolor.html" class="function">imagecreatetruecolor()</a></span> 创建的。
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.imagecopyresampled-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <span class="function"><a href="imagecopyresized.html" class="function" rel="rdfs-seeAlso">imagecopyresized()</a> - 拷贝部分图像并调整大小</span>
  </p>
 </div>

</div></div></div></body></html>